import { getCollection } from "astro:content";
import useIcon from "@/hooks/useIcon";
import ColorMap from "@/styles/PersonalThemeColor";
import LiverOrder from "@/constant/LiverOrder";

export async function getClassCollect(
  judgeCb: (entry: CollectionEntry<"wiki">) => boolean
) {
  // 顺序由：https://www.pdplive.net/members/all 期数顺序决定
  return await getCollection("wiki", (entry) => {
    return !entry.data.draft && judgeCb(entry);
  });
}
export const BasicList = await Promise.all(
  [
    ["blocks", "方块"],
    ["effects", "状态效果"],
    ["enchantments", "附魔"],
    ["entities", "生物/实体"],
    ["biomes", "生物群系"],
  ].map(async ([v, name]) => {
    return {
      path: v,
      name,
      list: (
        await getClassCollect((entry) => {
          return entry.slug.includes(v);
        })
      ).map((v) => {
        const icon = useIcon(v.slug.split("/").pop())!;
        return {
          ...icon,
          name: v.data.name || icon.name,
          smallIcon: icon?.smallIcon || v.data.property?.img,
          link: `/wiki/${v.slug}`,
        };
      }),
    };
  })
);

export const ItemList: Record<string, any[]> = {};
(
  await getClassCollect((entry) => {
    return entry.slug.includes("items");
  })
).map((v) => {
  const [_1, season, liver, item] = v.slug.split("/");

  switch (season) {
    case "other":
    case "sponser":
      // eslint-disable-next-line no-case-declarations
      const icon = useIcon(liver)!;
      return {
        liver: season,
        item: {
          ...icon,
          name: v.data.name || icon.name,
          link: `/wiki/items/${liver}`,
        },
      };
  }
  const icon = useIcon(item)!;

  return {
    ...ColorMap[liver as keyof typeof ColorMap],
    liver,
    item: {
      ...icon,
      name: v.data.name || icon.name,
      link: `/wiki/items/${item}`,
    },
  };
}).sort((v1, v2) =>
  // @ts-expect-error: liver always exist
  LiverOrder.indexOf(v1.liver) < LiverOrder.indexOf(v2.liver) ? 1 : -1
).map((v) => {
  ItemList[v.liver] ? ItemList[v.liver].push(v) : (ItemList[v.liver] = [v]);
});
